###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/Templates.pm.pl -> Templates module                            #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package CP::Templates;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Error.pm.pl";
require "Standard.pm.pl";
require "Authenticate.pm.pl";

use File::Find;
use Cwd;
use strict;

require "ControlPanel/Output/Templates.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #
  
  &Authenticate::CP(DB => $in{'DB'}, ACTION => "TEMPLATESETS_VIEW");

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  if (!$SL::QUERY{'TID'}) {
    &Error::Error("CP", MESSAGE => "You didn't specify a TemplateSet ID (TID)");
  }
  
  my $TemplateSet = $in{'DB'}->BinarySelect(
    TABLE => "TemplateSets",
    KEY   => $SL::QUERY{'TID'}
  );
  unless ($TemplateSet) {
    &Error::Error("CP", MESSAGE => "The TemplateSet ID (TID) you specified is invalid");
  }

  #----------------------------------------------------------------------#
  # Getting data...                                                      #

  my $Cwd = cwd();

  opendir(DIR, "$SL::PATH/Private/Skins/Templates/$SL::QUERY{'TID'}") || &Error::CGIError("Error opening directory. $!", "$SL::PATH/Private/Skins/Templates/$SL::QUERY{'TID'}");
  my @Files = readdir(DIR);
  closedir(DIR);

  my @Children;
  
  foreach my $file (@Files) {
    next if ($file =~ /^\.\.?$/);
    if (-d "$SL::PATH/Private/Skins/Templates/$SL::QUERY{'TID'}/$file") {
      push(@Children, {
        name      => $file,
        icon      => "folder.gif",
        children  => $self->myExplore($file)
      });
    } else {
      push(@Children, {
        name      => $file,
        icon      => "file.gif",
        namelink  => { url => "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=ModifyTemplate&TID=$SL::QUERY{'TID'}&Template=$file&SID=$SL::ADMIN{'SID'}" }
      });
    }
  }

  my %INPUT;
  
  $INPUT{'TREE'} = \@Children;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::Templates->new();

  &Standard::PrintHTMLHeader();
  print $Skin->show(input => \%INPUT);
  
  return 1;
}

###############################################################################
# myExplore subroutine
sub myExplore {
  my $self = shift;
  my ($Dir) = @_;
  
  opendir(DIR, "$SL::PATH/Private/Skins/Templates/$SL::QUERY{'TID'}/$Dir") || &Error::CGIError("Error opening directory. $!", "$SL::PATH/Private/Skins/Templates/$SL::QUERY{'TID'}/$Dir");
  my @Files = readdir(DIR);
  closedir(DIR);
  
  my @Children;
  
  foreach my $file (@Files) {
    next if ($file =~ /^\.\.?$/);
    if (-d "$SL::PATH/Private/Skins/Templates/$SL::QUERY{'TID'}/$Dir/$file") {
      push(@Children, {
        name      => $file,
        icon      => "folder.gif",
        children  => $self->myExplore("$Dir/$file")
      });
    } else {
      push(@Children, {
        name      => $file,
        icon      => "file.gif",
        namelink  => { url => "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=ModifyTemplate&TID=$SL::QUERY{'TID'}&Template=$Dir/$file&SID=$SL::ADMIN{'SID'}" }
      });
    }
  }
  
  return \@Children;
}

1;